/**
 * 中医馆页面菜单
 * 
 */
//获取加载的中医馆id和name
//var orgName=$('#orgName').val();
var guid=$('#guid').val();
var detailTabs = "#tcmMainTabs";
var detailLayout = "#tcmDetailLayout";
orange.detailPage = {	//	中医馆应用主页
		isCollapse: false,
		hideWest: function() {	//	隐藏西部面板,暂时未用到，再次优化的话可以把中医馆的左侧菜单收起来
			$(detailLayout).layout('collapse', 'west');
		},
		getWidth:function(){
			var width=($(detailTabs).width())*0.75>1000?($(detailTabs).width())*0.75:1000;
			return width;
		},
		getHeight:function(){
			var height=($(detailTabs).height())*0.65>285?($(detailTabs).height())*0.65:285;
			return height;
		}
};
//获取中医馆菜单项
	var data = orange.getTCMDetailResourceData();
	var divcontent='';
	//横向菜单id
	var content='',panel = $("#leftMenu").panel();
	for(var i=0; i<data.length; i++) {
		var title = data[i].name;
		var href=data[i].url;
		var iconCls = data[i].icon;
		var children = data[i].children;
		if(children.length==0){
			content += '<a href="#" class="easyui-linkbutton" data-options="iconCls:\''+iconCls+' color-icon\'" onclick=_indexMainTabs.addModule(\''+title+'\',\''+href+'\')>'+title+'</a>';
		}else{
			var menu = "#menu_"+ data[i].id;
			content += '<a href="#" class="easyui-menubutton" data-options="menu:\''+menu+'\',iconCls:\''+iconCls+' color-icon\'">'+title+'</a>';
			var childcontent=renderChildrenData (children);
			content += '<div id="menu_'+data[i].id+'">'+childcontent+'</div>';
		}
		divcontent='';
	}
	$("#leftMenu").panel('clear');
	$("#leftMenu").panel('body').append('<div>'+content+'</div>');
	$.parser.parse(panel);
//横向菜单展示调用方法
	function renderChildrenData (childlist) {
		for(var i=0;i<childlist.length;i++) {
			var menucontent='';
			var url=childlist[i].url;
			var name = childlist[i].name;
			var icon = childlist[i].icon;
			var child = childlist[i].children;
			if(child.length==0){
				menucontent += '<div  data-options="iconCls:\''+icon+' color-icon\'" onclick=_indexdetailTabs.addModule(\''+name+'\',\''+url+'\')>'+name+'</div>';
				divcontent += menucontent;
			}else{
				divcontent += '<div><span>'+name+'</span><div>';
				renderChildrenData(child);
				divcontent += '</div></div>';
			}
		}
		return divcontent;
	}
		
var _indexdetailTabs = orange.detailPage.detailTabs = {	//	初始化标签栏
	addModule: function(title, href, iconCls, iniframe, closable, refreshable, selected) {	//	标签栏增加一个模块
		var opts = orange.detailPage.detailTabs.parseCreateTabArgs(arguments);
		var isExists = orange.detailPage.detailTabs.isExists(opts.title, opts.href);
        switch (isExists) {
            case 0: orange.detailPage.detailTabs.createTab(opts); break;
            case 1: orange.detailPage.detailTabs.createTab(opts); break;
            case 2: orange.detailPage.detailTabs.jumpTab(opts.title); break;
            default: break;
        }
	},
	createTab: function(title, href, iconCls, iniframe, closable, refreshable, selected, pre) {	//	创建一个标签页
		var t = $(detailTabs), i = 0, opts = orange.detailPage.detailTabs.parseCreateTabArgs(arguments);
        while (t.tabs("getTab", opts.title + (i ? String(i) : ""))) { i++; }
        t.tabs("add", {title:opts.title, url:opts.href, iconCls:opts.iconCls, iniframe:opts.iniframe, closable:opts.closable, refreshable:opts.refreshable, selected:opts.selected});
        loadPage({url: opts.href, selector: orange.getAppAjaxLoadFlag(), callback: function(pageContent) {	//	加载页面
            if(pageContent != undefined && pageContent != "") {
            	t.tabs('getTab', opts.title).append(pageContent);
                $.parser.parse(t.tabs('getTab', opts.title));
            }
        }, asyncCallback: function() {	//	异步加载脚本文件回调函数处理
        	_indexdetailTabs.preHandler(pre);
        }});
	},
	isExists: function(title, href) {	//	判断指定的选项卡是否存在于当前主页面的选项卡组中
		var t = $(detailTabs), tabs = t.tabs("tabs"), array = $.array.map(tabs, function (val) { return val.panel("options"); }),
        list = $.array.filter(array, function (val) { return val.title == title; }), ret = list.length ? 1 : 0;
        if (ret && $.array.some(list, function (val) { return val.url == href; })) { ret = 2; }
        return ret;
	},
	jumpTab: function(which, preHandler) {	//	标签页跳转
		$(detailTabs).tabs("select", which);
		this.preHandler(preHandler);
	},
	parseCreateTabArgs: function(args) {	//	解析方法参数
		var ret = {};
        if (!args || !args.length) {
            $.extend(ret, orange.detailPage.detailTabs.tabDefaultOption);	//	得到合并的结果却又不想修改ret的结构
        } else if (args.length == 1) {
            $.extend(ret, orange.detailPage.detailTabs.tabDefaultOption, typeof args[0] == "object" ? args[0] : { title: args[0] });
        } else if (args.length == 2) {
            $.extend(ret, orange.detailPage.detailTabs.tabDefaultOption, { title: args[0], href: args[1]});
        } else if (args.length == 3) {
            $.extend(ret, orange.detailPage.detailTabs.tabDefaultOption, { title: args[0], href: args[1], iconCls: args[2] });
        } else if (args.length == 4) {
            $.extend(ret, orange.detailPage.detailTabs.tabDefaultOption, { title: args[0], href: args[1], iconCls: args[2], iniframe: args[3] });
        } else if (args.length == 5) {
            $.extend(ret, orange.detailPage.detailTabs.tabDefaultOption, { title: args[0], href: args[1], iconCls: args[2], iniframe: args[3], closable: args[4] });
        } else if (args.length == 6) {
            $.extend(ret, orange.detailPage.detailTabs.tabDefaultOption, { title: args[0], href: args[1], iconCls: args[2], iniframe: args[3], closable: args[4], refreshable: args[5] });
        } else if (args.length >= 7) {
            $.extend(ret, orange.detailPage.detailTabs.tabDefaultOption, { title: args[0], href: args[1], iconCls: args[2], iniframe: args[3], closable: args[4], refreshable: args[5], selected: args[6] });
        }
        return ret;
	},                                                                                    
	preHandler: function(preHandler) {	//	脚本预处理
		if(typeof preHandler == 'object' && preHandler.name) {
			var fn = eval(preHandler.name);
			if(typeof fn === 'function') {
				fn.call(this, preHandler.params);
			}
		}
	},
	tabDefaultOption: {	//	选项卡默认配置
		title: "新建选项卡", href: "", iniframe: true, closable: true, refreshable: true, repeatable: true, selected: true
	}
};
loadPage({id:'tcmHomePanel', url:'tcmdetail/home' , selector: 'tcmhomepage', callback: function() {	
	$.parser.parse($(detailTabs).tabs('getTab', 0));
}});
//跳转新建主页
//orange.detailPage.detailTabs.createTab('主页','tcmdetail/home','', true, false, true, true);
